package com.lkx.web;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.lkx.domain.User;
import com.lkx.service.ApiSubjectService;
import com.lkx.service.UserService;


@Controller
@RequestMapping("back")
public class LoginController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private ApiSubjectService apiSubjectService;


    @RequestMapping("/login")
    public ModelAndView login(String loginId, String loginPwd, String yzmCode, HttpSession session) {
        ModelAndView view = new ModelAndView("/back/login");

        if (loginId != null && loginPwd != null && yzmCode != null) {
            if (session.getAttribute("validateCode") == null) return view;

            String yzm = session.getAttribute("validateCode").toString();
            if (yzmCode.equals(yzm)) {

                UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginId, loginPwd);
                Subject subject = SecurityUtils.getSubject();
                try {
                    subject.login(usernamePasswordToken);   //完成登录
                    User user = (User) subject.getPrincipal();
                    session.setAttribute("userName", user.getUserName());
                    session.setAttribute("loginId", loginId);
                    session.setAttribute("user", user);
                    view = new ModelAndView("/back/index");
                    view.addObject("userName", user.getUserName());
                    view.addObject("loginId", loginId);
                    view.addObject("subjectList", apiSubjectService.list(user.getId()).getList());

                    return view;
                } catch (Exception e) {
                    view.addObject("msg", "用户不存在");
                    return view;//返回登录页面
                }
				
				
				/*User user = userService.selectByLoginId(loginId);
				if(user == null) {
					view.addObject("msg", "用户不存在");
					return view;
				}
				if (loginId.equals(user.getPhone()) && loginPwd.equals(user.getPassword())) {
					logger.info(loginId + "登录成功");
					session.setAttribute("userName", user.getUserName());
					session.setAttribute("loginId", loginId);
					session.setAttribute("user", user);
					view = new ModelAndView("back/index");
					view.addObject("userName",user.getUserName());
					view.addObject("loginId",loginId);
					view.addObject("subjectList", apiSubjectService.list(user.getId()).getList());
				} else {
					view.addObject("msg", "用户名或密码不正确！");
				}*/
            } else {
                view.addObject("msg", "验证码不正确！");
            }
        }

        return view;
    }

    @RequestMapping("/tologin")
    public ModelAndView tologin(String loginId, String loginPwd, HttpSession session) {
        ModelAndView view = new ModelAndView("back/login");
        User model = userService.selectByLoginId(loginId);
        if (model != null && loginPwd.equals(model.getPassword())) {
            view = new ModelAndView("back/index");
            session.setAttribute("userName", model.getUserName());
            session.setAttribute("loginId", loginId);
            view = new ModelAndView("back/index");
            view.addObject("userName", model.getUserName());
            view.addObject("loginId", loginId);
        }
        return view;
    }

    @RequestMapping("/loginout")
    public ModelAndView loginout(HttpSession session) {
        ModelAndView view = new ModelAndView("back/login");
        session.setAttribute("userName", null);
        return view;
    }


    @RequestMapping("")
    public String index() {
        return "back/index";
    }

}
